package ex.mlist;



import java.util.ArrayList;
import java.util.List;

import Database.DBHelper;
import Database.Ringtone;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class ManagerActivity1 extends ListActivity {
    /** Called when the activity is first created. */
	
	 AlertDialog.Builder alert;
	 ListView r_list;
	 List<String> lis;
	 int count;
	 MediaPlayer mMediaPlayer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.managerlayout);
           lis=new ArrayList<String>();
        r_list = getListView();
        mMediaPlayer=new MediaPlayer();
        r_list.setOnItemLongClickListener(new OnItemLongClickListener() {
        	
        	
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int position=arg2;
				alert=new AlertDialog.Builder(ManagerActivity1.this);
				alert.setTitle("Delete");
		    	alert.setMessage("Confrim delete ");
		    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		    		
					public void onClick(DialogInterface dialog, int which) {
						cancel(position);
						refreshList();
					}
				});
		    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
		    		
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		    	alert.create().show();
				return true;
			}
		});
        
        refreshList();
        
        
     
   }
    
    
    
    
    public void addButtonHandler(View v)
    {
    	Intent intent=new Intent(ManagerActivity1.this, MlistActivity.class);
		startActivity(intent);
    }
    
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
		System.gc();
		//Toast.makeText(this,"djshjd",5).show();
		try {

			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.reset();
			}
            System.out.println(lis.get(position).toString());
			mMediaPlayer.setDataSource("/mnt/sdcard/"+lis.get(position).toString());
			mMediaPlayer.prepare();
			mMediaPlayer.start();
		}

		catch (Exception e) {

		}
	}
    
    protected void onStop()
	 {
    	super.onStop();
		 mMediaPlayer.stop();
		 
	 }
    private void refreshList() {
    	
    	SharedPreferences tones = getSharedPreferences("MYLIST", 0);
		 String [] songs={""};
		 int listcount,i;
		 listcount=tones.getInt("count", 0);		 
		 System.out.println(listcount);
		 for(i=0;i<listcount;i++)
		 {
			lis.add(tones.getString(i+"", "")); 
		 }
		 //songs[listcount]=listcount+"";
		 if(listcount==0){
			 System.out.println(listcount);
			 lis.add("");
		 }
        r_list.setAdapter(new ToneAdapter(getApplicationContext(),lis));
        
	}


    public void cancel(int number)
	{
		
    	SharedPreferences tones = getSharedPreferences("MYLIST", 0);
    	SharedPreferences.Editor editor=tones.edit();
    	int i,count;
    	count=tones.getInt("Count", 0);
    	for(i=number;i<count;i++){
    		editor.putString(i+"", tones.getString((i+1)+"", ""));
    	}
    	editor.putInt("count", count-1);
    	editor.commit();
	}



	
    
    public boolean isIntentAvailable(Intent intent) {
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}